define(function (require, exports, module) {
	require("handlebars");
   /*require("layer-css");
    require("layer");*/

    $.fn.tpl = function (options, func) {
        var options = $.extend(options);
        options.type = options.type || "tpl";
        options.json = options.json || {};
        options.source = options.source || "";

        var _this = this;
        var source = options.source;
        if (options.type === "tpl") {
            source = require(options.source) || "";
        }
        var targetHtml = Handlebars.compile(source)(options.json);
        _this.html(targetHtml);
        if (typeof(func) === "function") {
            func.apply(_this);
        }
    }
    $.extend({
        showWarnDialogWithClose: function (msg, okFun, option) {
            var html = "<div class='tipsWrap w450'>" + "<div class='tipsTxt'>" + "<i class='iconfont tipIco'>" + "&#xe63c;" + "</i><span>" + msg + "</span></div>" + "<div class='tipsBtnBar'><a href='javascript:' class='_qd_dialog_ok okBtn'>确定</a></div></div>"
            showDialog(true, html, okFun, option);
        },
        showWarnDialogWithOutClose: function (msg, okFun, option) {
            var html = "<div class='tipsWrap w450'>" + "<div class='tipsTxt'>" + "<i class='iconfont tipIco'>" + "&#xe63c;" + "</i><span>" + msg + "</span></div>" + "<div class='tipsBtnBar'><a href='javascript:' class='_qd_dialog_ok okBtn'>确定</a></div></div>"
            showDialog(false, html, okFun, option);
        },
        showErrDialogWithClose: function (msg, okFun, option) {
            var html = '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>'+msg+'</span></div><div class="tipsBtnBar"><a href="javascript:" class="_qd_dialog_ok okBtn">确定</a></div></div>';
            showDialog(true,html,okFun,option);
        },
        showErrDialogWithOutClose: function (msg, okFun, option) {
            var html = '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>' + msg + '</span></div><div class="tipsBtnBar"><a href="javascript:" class="_qd_dialog_ok okBtn">确定</a></div></div>';
            showDialog(false, html, okFun, option);
        },
        showDialogNeedLogin: function (msg, okFun, option) {
            var html = '<div class="fileconfirm"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>' + msg + '</span></div><div class="fileconfirmbtn"><a href="/user/login.html" class="okBtn">重新登录</a><a class="cancleBtn _qd_dialog_cancel">取消</a></div></div>';
            showDialog(false, html, okFun, option);
        }
    });

    function showDialog(closeBtn, html, okFun, option) {
        option = option || {};
        var options = $.extend(option);
        options.dtype = option.type || 1;
        options.title = option.title || "&nbsp;";
        options.area = option.area || ['450px', '190px'];
        options.border = option.border || [1, 1, '#cecfd0'];
        options.closeBtn = closeBtn;
        //构造确认框DOM
        var _qd_layer = $.layer({
            type: options.dtype,
            closeBtn: [0, options.closeBtn],
            title: options.title,
            area: options.area,
            border: options.border,
            page: {
                html: html
            }
        });

        $("._qd_dialog_ok").unbind('click').click(function () {
            if (typeof(okFun) === "function") {
                if(okFun(_qd_layer)){
                    layer.close(_qd_layer);
                }
            } else {
                layer.close(_qd_layer);
            }
        });
        $("._qd_dialog_cancel").unbind('click').click(function () {
            layer.close(_qd_layer);
        });
    }

    //exports.tpl = $.fn.tpl;
    //exports.showDialogWithClose = $.showWarnDialogWithClose;
    //exports.showDialogWithOutClose = $.showWarnDialogWithOutClose;
    //exports.showErrDialogWithClose = $.showErrDialogWithClose;
    //exports.showErrDialogWithOutClose = $.showErrDialogWithOutClose;
    //
    //exports.showDialogNeedLogin = $.showDialogNeedLogin;
});